package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    /*
    * 订单明细
    *
    * */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam int page, int pageSize,String number){
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //条件构造
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件,按照结账时间倒叙查询
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        //添加条件,如果number不等于null,就按照number作为条件模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        //调用方法查询
        orderService.page(ordersPage,queryWrapper);
        //返回查到的数据
        return R.success(ordersPage);
    }
}